package com.geniusk.app.utils;

import com.geniusk.common.utils.RedisUtil;
import com.geniusk.security.utils.SecurityUtil;
import com.geniusk.system.model.ro.FollowUserRO;
import com.geniusk.system.model.vo.PublicUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

import static com.geniusk.common.constant.RedisConstants.FOLLOW_USER_KEY;

@Component
public class FollowUserUtil {

    @Autowired
    private RedisUtil redisUtil;

    public void setFollow(PublicUserVO publicUserVO){
        String followUserKey = getFollowUserKey();
        FollowUserRO followUserRO = redisUtil.getCacheObject(followUserKey);
        if(followUserRO != null){
            Map<Long, Date> userIdMap = followUserRO.getUserIdMap();
            boolean follow = userIdMap.containsKey(publicUserVO.getUserId());
            publicUserVO.setFollow(follow);
            if(follow){
                publicUserVO.setFollowTime(userIdMap.get(publicUserVO.getUserId()));
            }
        }else {
            publicUserVO.setFollow(false);
        }
    }

    /**
     * 获取用户关注缓存Key
     * @return 关注粉丝缓存Key
     */
    private String getFollowUserKey(){
        return FOLLOW_USER_KEY + SecurityUtil.getUserId();
    }
}
